<template>
  <div class="box">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avater">
        <img src="../assets/2.jpeg">
      </div>
      <!--  表单区域 -->
      <el-form class="login_form" ref="formRef" :model="form" :rules="FormRules">
        <el-form-item prop="username">
          <el-input v-model="form.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="username">
          <el-input v-model="form.password" type="password" prefix-icon="iconfont icon-3702mima"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="onLogin">登录</el-button>
          <el-button type="primary" @click="resFrom">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      form: {
        username: 'admin',
        password: '123456'
      },
      // 表单校验
      FormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 7, message: '长度在 2 到 7 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 2, max: 7, message: '长度在 2 到 7 个字符', trigger: 'blur' }
        ]
      }
    }
  },

  created () {

  },

  methods: {
    // 表单重置
    resFrom () {
      this.$refs.formRef.resetFields()
    },
    // 登录
    onLogin () {
      this.$refs.formRef.validate(async flag => {
        if (!flag) return
        const { data: res } = await this.$axios.post('login', this.form)
        if (res.meta.status !== 200) return this.$message.console.error('登录失败')
        sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style scoped lang='less'>
    .box {
    width: 100%;
    height: 100%;
    background: url(../assets/0048.gif) no-repeat;
    background-size: 100%;
    .login_box {
        width: 450px;
        height: 300px;
        background: rgba(255, 255, 255,0.5);
        border-radius: 5px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        .avater {
            width: 100px;
            height: 100px;
             border-radius: 50%;
             box-shadow: 0px 0px 15px 5px orange;
             padding: 5px;
             overflow: hidden;
             position: absolute;
             left: 50%;
             transform: translate(-50%, -50%);
             background: rgba(100, 18, 18, 0.5);
             img {
                 width: 100%;
                 height: 100%;
                  border-radius: 50%;
             }
        }
        .login_form {
          width: 100%;
          position: absolute;
          bottom: 0;
          padding: 0 20px;
          box-sizing: border-box;
          .btns {
            display: flex;
            justify-content: flex-end;
          }
        }
    }
}
</style>
